package com.wry.sharding.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.wry.sharding.dao.Attendance;
import com.wry.sharding.service.AttendanceService;
import com.wry.sharding.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <h1>  <h1>
 *
 * @author wry
 * @since 2025/1/10 16:51
 */
@Slf4j
@RequestMapping("test2")
@RestController
public class ShardingByDateTestController {

    @Resource
    private AttendanceService attendanceService;

    @GetMapping("add-attendance")
    public String t1(@RequestParam("accessDate") String accessDate) {
        for (int i = 1; i <= 10; i++) {
            Attendance attendance = new Attendance();
            attendance.setUserId(1);
            attendance.setSiteName("广州市换购去-"+i);
            attendance.setAccessDate(TimeUtil.toLocalDate(accessDate));
            attendance.setInsertTime(LocalDateTime.now());
            attendanceService.save(attendance);
            log.info("insert:" + attendance);
        }
        return "ok";
    }

    @GetMapping("get-attendance")
    public Object t1(@RequestParam("userId") Integer userId, @RequestParam("accessDate") String accessDate) {
        final List<Attendance> list = attendanceService.lambdaQuery()
                .eq(Attendance::getUserId, userId)
                .eq(Attendance::getAccessDate, TimeUtil.toLocalDate(accessDate))
                .list();
        return JSON.toJSON(list);
    }
}
